__author__ = 'st316'
'''
Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
'''


# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

    def __repr__(self):
        return self.val

    def toString(self):
        cur = self
        s = ''
        while cur.next:
            s += str(cur.val) + ' -> '
            cur = cur.next
        return s + str(cur.val)


class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def deleteDuplicates(self, head):
        if not head or not head.next:
            return head
        cur = head
        post = cur.next
        while post:
            if post.val == cur.val:
                post = post.next
            else:
                cur.next = post
                cur = post
        cur.next = post
        return head


if __name__ == '__main__':
    l = [1, 1]
    ll = ListNode(l[0])
    cur = ll
    for v in l[1:]:
        cur.next = ListNode(v)
        cur = cur.next
    s = Solution()
    print s.deleteDuplicates(ll).toString()